package com.sjq.demo;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.AccessLog;
import org.junit.Test;

@Slf4j
public class tree {


    @Test
    public void testTreeNode(){
        String data = "{\"errcode\":0,\"errmsg\":\"ok\",\"department\":[{\"id\":1,\"name\":\"苏格通-苏州\",\"parentid\":0,\"order\":100000000},{\"id\":209,\"name\":\"湖桥村4\",\"parentid\":19,\"order\":99991000},{\"id\":208,\"name\":\"湖桥村2\",\"parentid\":19,\"order\":99992000},{\"id\":207,\"name\":\"东村村6\",\"parentid\":155,\"order\":99995000},{\"id\":206,\"name\":\"林屋村1\",\"parentid\":24,\"order\":99995000},{\"id\":205,\"name\":\"东河社区3\",\"parentid\":110,\"order\":99995000},{\"id\":204,\"name\":\"三山村1\",\"parentid\":39,\"order\":99996000},{\"id\":203,\"name\":\"石塘村5\",\"parentid\":132,\"order\":99996000},{\"id\":202,\"name\":\"浦庄村1\",\"parentid\":74,\"order\":99997000},{\"id\":201,\"name\":\"林屋村6\",\"parentid\":24,\"order\":99996000},{\"id\":200,\"name\":\"浦庄村3\",\"parentid\":74,\"order\":99998000},{\"id\":199,\"name\":\"衙甪里村1\",\"parentid\":53,\"order\":99995000},{\"id\":198,\"name\":\"洞庭社区2\",\"parentid\":13,\"order\":99998000},{\"id\":197,\"name\":\"前塘村2\",\"parentid\":78,\"order\":99997000},{\"id\":196,\"name\":\"湖桥村1\",\"parentid\":19,\"order\":99993000},{\"id\":195,\"name\":\"新潦村2\",\"parentid\":93,\"order\":99998000},{\"id\":194,\"name\":\"牛桥村2\",\"parentid\":55,\"order\":99998000},{\"id\":193,\"name\":\"东村村5\",\"parentid\":155,\"order\":99996000},{\"id\":192,\"name\":\"渡口村2\",\"parentid\":17,\"order\":99999000},{\"id\":191,\"name\":\"东村村2\",\"parentid\":155,\"order\":99997000},{\"id\":190,\"name\":\"石舍村1\",\"parentid\":37,\"order\":99999000},{\"id\":189,\"name\":\"联盟社区1\",\"parentid\":104,\"order\":99999000},{\"id\":188,\"name\":\"石塘村4\",\"parentid\":132,\"order\":99997000},{\"id\":187,\"name\":\"三山村5\",\"parentid\":39,\"order\":99997000},{\"id\":186,\"name\":\"秉常村4\",\"parentid\":21,\"order\":99997000},{\"id\":185,\"name\":\"元山村3\",\"parentid\":7,\"order\":99997000},{\"id\":184,\"name\":\"太湖村3\",\"parentid\":41,\"order\":99998000},{\"id\":183,\"name\":\"界路村2\",\"parentid\":45,\"order\":99996000},{\"id\":182,\"name\":\"太湖村1\",\"parentid\":41,\"order\":99999000},{\"id\":181,\"name\":\"庭山村1\",\"parentid\":5,\"order\":99996000},{\"id\":180,\"name\":\"灵湖村2\",\"parentid\":83,\"order\":99999000},{\"id\":179,\"name\":\"石公村5\",\"parentid\":28,\"order\":99994000},{\"id\":178,\"name\":\"陆舍村2\",\"parentid\":67,\"order\":99998000},{\"id\":177,\"name\":\"东村村4\",\"parentid\":155,\"order\":99998000},{\"id\":176,\"name\":\"牛桥村3\",\"parentid\":55,\"order\":99999000},{\"id\":175,\"name\":\"湖桥村8\",\"parentid\":19,\"order\":99994000},{\"id\":174,\"name\":\"前塘村4\",\"parentid\":78,\"order\":99998000},{\"id\":173,\"name\":\"潦里村1\",\"parentid\":170,\"order\":99999000},{\"id\":172,\"name\":\"衙甪里村5\",\"parentid\":53,\"order\":99996000},{\"id\":171,\"name\":\"潦里村2\",\"parentid\":170,\"order\":100000000},{\"id\":170,\"name\":\"潦里村\",\"parentid\":12,\"order\":99988000},{\"id\":169,\"name\":\"石公村3\",\"parentid\":28,\"order\":99995000},{\"id\":168,\"name\":\"莫厘村3\",\"parentid\":60,\"order\":99998000},{\"id\":167,\"name\":\"碧螺村3\",\"parentid\":47,\"order\":99998000},{\"id\":166,\"name\":\"石庄村2\",\"parentid\":10,\"order\":99995000},{\"id\":165,\"name\":\"东河社区1\",\"parentid\":110,\"order\":99996000},{\"id\":164,\"name\":\"东村村1\",\"parentid\":155,\"order\":99999000},{\"id\":163,\"name\":\"石公村2\",\"parentid\":28,\"order\":99996000},{\"id\":162,\"name\":\"堂里村3\",\"parentid\":85,\"order\":99998000},{\"id\":161,\"name\":\"石塘村1\",\"parentid\":132,\"order\":99998000},{\"id\":160,\"name\":\"渡桥村2\",\"parentid\":35,\"order\":99998000},{\"id\":159,\"name\":\"缥缈村3\",\"parentid\":32,\"order\":99997000},{\"id\":158,\"name\":\"湖桥村7\",\"parentid\":19,\"order\":99995000},{\"id\":157,\"name\":\"秉常村3\",\"parentid\":21,\"order\":99998000},{\"id\":156,\"name\":\"东村村3\",\"parentid\":155,\"order\":100000000},{\"id\":155,\"name\":\"东村村\",\"parentid\":4,\"order\":99989000},{\"id\":154,\"name\":\"石公村7\",\"parentid\":28,\"order\":99997000},{\"id\":153,\"name\":\"东蔡村4\",\"parentid\":49,\"order\":99996000},{\"id\":152,\"name\":\"陆巷村1\",\"parentid\":87,\"order\":99998000},{\"id\":151,\"name\":\"采莲村5\",\"parentid\":127,\"order\":99996000},{\"id\":150,\"name\":\"浦庄村2\",\"parentid\":74,\"order\":99999000},{\"id\":149,\"name\":\"蒋东村5\",\"parentid\":30,\"order\":99995000},{\"id\":148,\"name\":\"杨湾村3\",\"parentid\":89,\"order\":99998000},{\"id\":147,\"name\":\"石公村4\",\"parentid\":28,\"order\":99998000},{\"id\":146,\"name\":\"石塘村3\",\"parentid\":132,\"order\":99999000},{\"id\":145,\"name\":\"渡桥村1\",\"parentid\":35,\"order\":99999000},{\"id\":144,\"name\":\"三山村3\",\"parentid\":39,\"order\":99998000},{\"id\":143,\"name\":\"蒋东村4\",\"parentid\":30,\"order\":99996000},{\"id\":142,\"name\":\"吴巷村1\",\"parentid\":26,\"order\":99999000},{\"id\":141,\"name\":\"采莲村2\",\"parentid\":127,\"order\":99997000},{\"id\":140,\"name\":\"庭山村4\",\"parentid\":5,\"order\":99997000},{\"id\":139,\"name\":\"秉常村2\",\"parentid\":21,\"order\":99999000},{\"id\":138,\"name\":\"采莲村3\",\"parentid\":127,\"order\":99998000},{\"id\":137,\"name\":\"洞庭社区3\",\"parentid\":13,\"order\":99999000},{\"id\":136,\"name\":\"东蔡村3\",\"parentid\":49,\"order\":99997000},{\"id\":135,\"name\":\"农业园区1\",\"parentid\":134,\"order\":100000000},{\"id\":134,\"name\":\"农业园区\",\"parentid\":9,\"order\":99984000},{\"id\":133,\"name\":\"石塘村2\",\"parentid\":132,\"order\":100000000},{\"id\":132,\"name\":\"石塘村\",\"parentid\":9,\"order\":99985000},{\"id\":131,\"name\":\"衙甪里村4\",\"parentid\":53,\"order\":99997000},{\"id\":130,\"name\":\"采莲村1\",\"parentid\":127,\"order\":99999000},{\"id\":129,\"name\":\"林屋村5\",\"parentid\":24,\"order\":99997000},{\"id\":128,\"name\":\"采莲村4\",\"parentid\":127,\"order\":100000000},{\"id\":127,\"name\":\"采莲村\",\"parentid\":9,\"order\":99986000},{\"id\":126,\"name\":\"前塘村1\",\"parentid\":78,\"order\":99999000},{\"id\":125,\"name\":\"东河社区2\",\"parentid\":110,\"order\":99997000},{\"id\":124,\"name\":\"湖桥村3\",\"parentid\":19,\"order\":99996000},{\"id\":123,\"name\":\"东河社区6\",\"parentid\":110,\"order\":99998000},{\"id\":122,\"name\":\"渡村社区2\",\"parentid\":51,\"order\":99998000},{\"id\":121,\"name\":\"衙甪里村6\",\"parentid\":53,\"order\":99998000},{\"id\":120,\"name\":\"双湾村1\",\"parentid\":100,\"order\":99999000},{\"id\":119,\"name\":\"陆舍村1\",\"parentid\":67,\"order\":99999000},{\"id\":118,\"name\":\"湖桥村5\",\"parentid\":19,\"order\":99997000},{\"id\":117,\"name\":\"石庄村3\",\"parentid\":10,\"order\":99996000},{\"id\":116,\"name\":\"杨湾村2\",\"parentid\":89,\"order\":99999000},{\"id\":115,\"name\":\"东吴村4\",\"parentid\":15,\"order\":99996000},{\"id\":114,\"name\":\"莫厘村2\",\"parentid\":60,\"order\":99999000},{\"id\":113,\"name\":\"东吴村3\",\"parentid\":15,\"order\":99997000},{\"id\":112,\"name\":\"东河社区5\",\"parentid\":110,\"order\":99999000},{\"id\":111,\"name\":\"东河社区4\",\"parentid\":110,\"order\":100000000},{\"id\":110,\"name\":\"东河社区\",\"parentid\":4,\"order\":99990000},{\"id\":109,\"name\":\"东蔡村2\",\"parentid\":49,\"order\":99998000},{\"id\":108,\"name\":\"东吴村2\",\"parentid\":15,\"order\":99998000},{\"id\":107,\"name\":\"湖桥村6\",\"parentid\":19,\"order\":99998000},{\"id\":106,\"name\":\"界路村5\",\"parentid\":45,\"order\":99997000},{\"id\":105,\"name\":\"联盟社区2\",\"parentid\":104,\"order\":100000000},{\"id\":104,\"name\":\"联盟社区\",\"parentid\":9,\"order\":99987000},{\"id\":103,\"name\":\"庭山村3\",\"parentid\":5,\"order\":99998000},{\"id\":102,\"name\":\"堂里村1\",\"parentid\":85,\"order\":99999000},{\"id\":101,\"name\":\"双湾村2\",\"parentid\":100,\"order\":100000000},{\"id\":100,\"name\":\"双湾村\",\"parentid\":12,\"order\":99989000},{\"id\":99,\"name\":\"缥缈村1\",\"parentid\":32,\"order\":99998000},{\"id\":98,\"name\":\"缥缈村4\",\"parentid\":32,\"order\":99999000},{\"id\":97,\"name\":\"蒋东村2\",\"parentid\":30,\"order\":99997000},{\"id\":96,\"name\":\"陆巷村3\",\"parentid\":87,\"order\":99999000},{\"id\":95,\"name\":\"新潦村3\",\"parentid\":93,\"order\":99999000},{\"id\":94,\"name\":\"新潦村1\",\"parentid\":93,\"order\":100000000},{\"id\":93,\"name\":\"新潦村\",\"parentid\":12,\"order\":99990000},{\"id\":92,\"name\":\"界路村1\",\"parentid\":45,\"order\":99998000},{\"id\":91,\"name\":\"渡村社区3\",\"parentid\":51,\"order\":99999000},{\"id\":90,\"name\":\"杨湾村1\",\"parentid\":89,\"order\":100000000},{\"id\":89,\"name\":\"杨湾村\",\"parentid\":12,\"order\":99991000},{\"id\":88,\"name\":\"陆巷村2\",\"parentid\":87,\"order\":100000000},{\"id\":87,\"name\":\"陆巷村\",\"parentid\":12,\"order\":99992000},{\"id\":86,\"name\":\"堂里村2\",\"parentid\":85,\"order\":100000000},{\"id\":85,\"name\":\"堂里村\",\"parentid\":4,\"order\":99991000},{\"id\":84,\"name\":\"灵湖村1\",\"parentid\":83,\"order\":100000000},{\"id\":83,\"name\":\"灵湖村\",\"parentid\":9,\"order\":99988000},{\"id\":82,\"name\":\"石公村1\",\"parentid\":28,\"order\":99999000},{\"id\":81,\"name\":\"东吴村5\",\"parentid\":15,\"order\":99999000},{\"id\":80,\"name\":\"界路村4\",\"parentid\":45,\"order\":99999000},{\"id\":79,\"name\":\"前塘村3\",\"parentid\":78,\"order\":100000000},{\"id\":78,\"name\":\"前塘村\",\"parentid\":9,\"order\":99989000},{\"id\":77,\"name\":\"湖桥村9\",\"parentid\":19,\"order\":99999000},{\"id\":76,\"name\":\"石庄村6\",\"parentid\":10,\"order\":99997000},{\"id\":75,\"name\":\"浦庄村4\",\"parentid\":74,\"order\":100000000},{\"id\":74,\"name\":\"浦庄村\",\"parentid\":9,\"order\":99990000},{\"id\":73,\"name\":\"元山村4\",\"parentid\":7,\"order\":99998000},{\"id\":72,\"name\":\"林屋村4\",\"parentid\":24,\"order\":99998000},{\"id\":71,\"name\":\"元山村2\",\"parentid\":7,\"order\":99999000},{\"id\":70,\"name\":\"衙甪里村3\",\"parentid\":53,\"order\":99999000},{\"id\":69,\"name\":\"林屋村2\",\"parentid\":24,\"order\":99999000},{\"id\":68,\"name\":\"陆舍村3\",\"parentid\":67,\"order\":100000000},{\"id\":67,\"name\":\"陆舍村\",\"parentid\":9,\"order\":99991000},{\"id\":66,\"name\":\"园博园1\",\"parentid\":65,\"order\":100000000},{\"id\":65,\"name\":\"园博园\",\"parentid\":9,\"order\":99992000},{\"id\":64,\"name\":\"蒋东村1\",\"parentid\":30,\"order\":99998000},{\"id\":63,\"name\":\"石庄村1\",\"parentid\":10,\"order\":99998000},{\"id\":62,\"name\":\"庭山村5\",\"parentid\":5,\"order\":99999000},{\"id\":61,\"name\":\"莫厘村1\",\"parentid\":60,\"order\":100000000},{\"id\":60,\"name\":\"莫厘村\",\"parentid\":12,\"order\":99993000},{\"id\":59,\"name\":\"东蔡村1\",\"parentid\":49,\"order\":99999000},{\"id\":58,\"name\":\"碧螺村1\",\"parentid\":47,\"order\":99999000},{\"id\":57,\"name\":\"三山村4\",\"parentid\":39,\"order\":99999000},{\"id\":56,\"name\":\"牛桥村1\",\"parentid\":55,\"order\":100000000},{\"id\":55,\"name\":\"牛桥村\",\"parentid\":9,\"order\":99993000},{\"id\":54,\"name\":\"衙甪里村2\",\"parentid\":53,\"order\":100000000},{\"id\":53,\"name\":\"衙甪里村\",\"parentid\":4,\"order\":99992000},{\"id\":52,\"name\":\"渡村社区1\",\"parentid\":51,\"order\":100000000},{\"id\":51,\"name\":\"渡村社区\",\"parentid\":9,\"order\":99994000},{\"id\":50,\"name\":\"东蔡村5\",\"parentid\":49,\"order\":100000000},{\"id\":49,\"name\":\"东蔡村\",\"parentid\":4,\"order\":99993000},{\"id\":48,\"name\":\"碧螺村2\",\"parentid\":47,\"order\":100000000},{\"id\":47,\"name\":\"碧螺村\",\"parentid\":12,\"order\":99994000},{\"id\":46,\"name\":\"界路村3\",\"parentid\":45,\"order\":100000000},{\"id\":45,\"name\":\"界路村\",\"parentid\":9,\"order\":99995000},{\"id\":44,\"name\":\"农林大世界1\",\"parentid\":43,\"order\":100000000},{\"id\":43,\"name\":\"农林大世界\",\"parentid\":9,\"order\":99996000},{\"id\":42,\"name\":\"太湖村2\",\"parentid\":41,\"order\":100000000},{\"id\":41,\"name\":\"太湖村\",\"parentid\":12,\"order\":99995000},{\"id\":40,\"name\":\"三山村2\",\"parentid\":39,\"order\":100000000},{\"id\":39,\"name\":\"三山村\",\"parentid\":12,\"order\":99996000},{\"id\":38,\"name\":\"石舍村2\",\"parentid\":37,\"order\":100000000},{\"id\":37,\"name\":\"石舍村\",\"parentid\":9,\"order\":99997000},{\"id\":36,\"name\":\"渡桥村3\",\"parentid\":35,\"order\":100000000},{\"id\":35,\"name\":\"渡桥村\",\"parentid\":12,\"order\":99997000},{\"id\":34,\"name\":\"蒋东村3\",\"parentid\":30,\"order\":99999000},{\"id\":33,\"name\":\"缥缈村2\",\"parentid\":32,\"order\":100000000},{\"id\":32,\"name\":\"缥缈村\",\"parentid\":4,\"order\":99994000},{\"id\":31,\"name\":\"蒋东村6\",\"parentid\":30,\"order\":100000000},{\"id\":30,\"name\":\"蒋东村\",\"parentid\":4,\"order\":99995000},{\"id\":29,\"name\":\"石公村6\",\"parentid\":28,\"order\":100000000},{\"id\":28,\"name\":\"石公村\",\"parentid\":4,\"order\":99996000},{\"id\":27,\"name\":\"吴巷村2\",\"parentid\":26,\"order\":100000000},{\"id\":26,\"name\":\"吴巷村\",\"parentid\":12,\"order\":99998000},{\"id\":25,\"name\":\"林屋村3\",\"parentid\":24,\"order\":100000000},{\"id\":24,\"name\":\"林屋村\",\"parentid\":4,\"order\":99997000},{\"id\":23,\"name\":\"石庄村5\",\"parentid\":10,\"order\":99999000},{\"id\":22,\"name\":\"秉常村1\",\"parentid\":21,\"order\":100000000},{\"id\":21,\"name\":\"秉常村\",\"parentid\":4,\"order\":99998000},{\"id\":20,\"name\":\"湖桥村10\",\"parentid\":19,\"order\":100000000},{\"id\":19,\"name\":\"湖桥村\",\"parentid\":9,\"order\":99998000},{\"id\":18,\"name\":\"渡口村1\",\"parentid\":17,\"order\":100000000},{\"id\":17,\"name\":\"渡口村\",\"parentid\":12,\"order\":99999000},{\"id\":16,\"name\":\"东吴村1\",\"parentid\":15,\"order\":100000000},{\"id\":15,\"name\":\"东吴村\",\"parentid\":9,\"order\":99999000},{\"id\":14,\"name\":\"洞庭社区1\",\"parentid\":13,\"order\":100000000},{\"id\":13,\"name\":\"洞庭社区\",\"parentid\":12,\"order\":100000000},{\"id\":12,\"name\":\"东山镇\",\"parentid\":3,\"order\":99998000},{\"id\":11,\"name\":\"石庄村4\",\"parentid\":10,\"order\":100000000},{\"id\":10,\"name\":\"石庄村\",\"parentid\":9,\"order\":100000000},{\"id\":9,\"name\":\"临湖镇\",\"parentid\":3,\"order\":99999000},{\"id\":8,\"name\":\"元山村1\",\"parentid\":7,\"order\":100000000},{\"id\":7,\"name\":\"元山村\",\"parentid\":4,\"order\":99999000},{\"id\":6,\"name\":\"庭山村2\",\"parentid\":5,\"order\":100000000},{\"id\":5,\"name\":\"庭山村\",\"parentid\":4,\"order\":100000000},{\"id\":4,\"name\":\"金庭镇\",\"parentid\":3,\"order\":100000000},{\"id\":3,\"name\":\"吴中区\",\"parentid\":1,\"order\":100001000},{\"id\":210,\"name\":\"苏格通开发人员\",\"parentid\":1,\"order\":99999000}]}";
        JSONObject json = JSONObject.parseObject(data);
        JSONArray dataArr = json.getJSONArray("department");
        JSONArray resArr = treeUtil("0",dataArr);
        log.info(resArr.toJSONString());
    }

    public JSONArray treeUtil(String rootId,JSONArray arr){
        JSONArray rtArr = new JSONArray();
        JSONArray tempArr;
        for(int i=0;i<arr.size();i++){
            JSONObject obj = arr.getJSONObject(i);
            String pid = obj.getString("parentid");
            String id = obj.getString("id");
            String name = obj.getString("name");
            if(pid.equals(rootId)){
                JSONObject resObj = new JSONObject();
                resObj.put("id",id);
                resObj.put("name",name);
                resObj.put("pid",pid);
                tempArr = treeUtil(id,arr);
                if(tempArr.size()>0){
                    resObj.put("children",tempArr);
                }
                rtArr.add(resObj);
            }
        }
        return rtArr;
    }
}
